package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProjectSbclht;
import com.ruoyi.gcgl.domain.GcProjectSbclhtcrkd;
import com.ruoyi.gcgl.domain.GcProjectSbclhtcrkditem;
import com.ruoyi.gcgl.domain.GcProjectXmxq;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.service.IGcProjectSbclhtService;
import com.ruoyi.gcgl.service.IGcProjectSbclhtcrkdService;
import com.ruoyi.gcgl.service.IGcProjectSbclhtcrkditemService;
import com.ruoyi.gcgl.service.IGcProjectXmxqService;
import com.ruoyi.gcgl.service.IGcSupplyService;

/**
 * 设备材料合同出入库单Controller
 * 
 * @author ruoyi
 * @date 2022-12-03
 */
@Controller
@RequestMapping("/gcgl/sbclhtcrkd")
public class GcProjectSbclhtcrkdController extends BaseController
{
    private String prefix = "gcgl/sbclhtcrkd";

    @Autowired
    private IGcProjectSbclhtcrkdService gcProjectSbclhtcrkdService;
    @Autowired
    private IGcProjectSbclhtService gcProjectSbclhtService;
    @Autowired
    private IGcProjectSbclhtcrkditemService gcProjectSbclhtcrkditemService;
    @Autowired
    private IGcSupplyService gcSupplyService;
    @Autowired
    private IGcProjectXmxqService gcProjectXmxqService;
    
    @RequiresPermissions("gcgl:sbclhtcrkd:view")
    @GetMapping()
    public String sbclhtcrkd()
    {
        return prefix + "/sbclhtcrkd";
    }

    /**
     * 查询设备材料合同出入库单列表
     */
    @RequiresPermissions("gcgl:sbclhtcrkd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSbclhtcrkd gcProjectSbclhtcrkd)
    {
        startPage();
        gcProjectSbclhtcrkd.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        gcProjectSbclhtcrkd.setCreateUser(getLoginName());
        }
        List<GcProjectSbclhtcrkd> list = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdList(gcProjectSbclhtcrkd);
        return getDataTable(list);
    }

    /**
     * 导出设备材料合同出入库单列表
     */
    @RequiresPermissions("gcgl:sbclhtcrkd:export")
    @Log(title = "设备材料合同出入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSbclhtcrkd gcProjectSbclhtcrkd)
    {
    	gcProjectSbclhtcrkd.setDeleteFlag(1);
    	if(getSysUser().isShow()){
            gcProjectSbclhtcrkd.setCreateUser(getLoginName());
            }
        List<GcProjectSbclhtcrkd> list = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdList(gcProjectSbclhtcrkd);
        ExcelUtil<GcProjectSbclhtcrkd> util = new ExcelUtil<GcProjectSbclhtcrkd>(GcProjectSbclhtcrkd.class);
        return util.exportExcel(list, "设备材料合同出入库单数据");
    }

    /**
     * 新增设备材料合同出入库单
     */
    @GetMapping("/add/{sbclhtId}")
    public String add(@PathVariable("sbclhtId") Long sbclhtId, ModelMap mmap)
    {
    	GcProjectSbclht gcProjectSbclht = gcProjectSbclhtService.selectGcProjectSbclhtById(sbclhtId);
        mmap.put("gcProjectSbclht", gcProjectSbclht);
        mmap.put("riq", new Date());
        GcSupply gcSupply = new GcSupply();
        gcSupply.setDeleteFlag(1);
        gcSupply.setType(4);
        List<GcSupply> supplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        mmap.put("supplylis", supplylis);
        
        GcProjectXmxq gcProjectXmxq = new GcProjectXmxq();
        gcProjectXmxq.setGcProjectid(gcProjectSbclht.getGcProjectid());
        List<GcProjectXmxq> xmxqlis = gcProjectXmxqService.selectGcProjectXmxqList(gcProjectXmxq);
        mmap.put("xmdz", xmxqlis.get(0).getXmdz());
        return prefix + "/add";
    }

    /**
     * 新增保存设备材料合同出入库单
     */
    @RequiresPermissions("gcgl:sbclhtcrkd:add")
    @Log(title = "设备材料合同出入库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectSbclhtcrkd gcProjectSbclhtcrkd){
    	try {
    		gcProjectSbclhtcrkd.setDeleteFlag(1);
        	gcProjectSbclhtcrkd.setStatus(3);
        	gcProjectSbclhtcrkd.setType(1);
        	gcProjectSbclhtcrkd.setCreateUser(getLoginName());
        	gcProjectSbclhtcrkd.setCreateAt(new Date());
        	gcProjectSbclhtcrkd.setUpdateUser(getLoginName());
        	gcProjectSbclhtcrkd.setUpdateAt(new Date());
//        	gcProjectSbclhtcrkd.setXmfzr(getLoginName());
        	gcProjectSbclhtcrkd.setXmfzrAt(new Date());
        	gcProjectSbclhtcrkdService.insertGcProjectSbclhtcrkd(gcProjectSbclhtcrkd);
        	
        	double hjje = 0.0;
        	//item
        	for(GcProjectSbclhtcrkditem item : gcProjectSbclhtcrkd.getItem()){
        		item.setSbclhtId(gcProjectSbclhtcrkd.getSbclhtId());
        		item.setSbclhtcrkdId(gcProjectSbclhtcrkd.getId());
        		item.setDeleteFlag(1);
        		item.setCreateAt(new Date());
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setGcProjectid(gcProjectSbclhtcrkd.getGcProjectid());
        		double jine = DecimalCalculateUtil.mul(Double.parseDouble(item.getDanj().toString()), Double.parseDouble(item.getShul()));
        		hjje += jine;
        		item.setJine(Double.valueOf(jine));
        		gcProjectSbclhtcrkditemService.insertGcProjectSbclhtcrkditem(item);
        	}
        	GcProjectSbclhtcrkd obj = new GcProjectSbclhtcrkd();
        	obj.setId(gcProjectSbclhtcrkd.getId());
        	obj.setHej(Double.valueOf(hjje));
        	gcProjectSbclhtcrkdService.updateGcProjectSbclhtcrkd(obj);
		} catch (Exception e) {
			// TODO: handle exception
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 修改设备材料合同出入库单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(id);
        
        GcProjectSbclhtcrkditem items = new GcProjectSbclhtcrkditem();
        items.setSbclhtcrkdId(id);
        List<GcProjectSbclhtcrkditem> itemlis= gcProjectSbclhtcrkditemService.selectGcProjectSbclhtcrkditemList(items);
        gcProjectSbclhtcrkd.setItem(itemlis);
        
        mmap.put("gcProjectSbclhtcrkd", gcProjectSbclhtcrkd);
        
        GcSupply gcSupply = new GcSupply();
        gcSupply.setType(4);
        gcSupply.setDeleteFlag(1);
        List<GcSupply> supplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        mmap.put("supplylis", supplylis);
        
        GcProjectXmxq gcProjectXmxq = new GcProjectXmxq();
        gcProjectXmxq.setGcProjectid(gcProjectSbclhtcrkd.getGcProjectid());
        List<GcProjectXmxq> xmxqlis = gcProjectXmxqService.selectGcProjectXmxqList(gcProjectXmxq);
        mmap.put("xmdz", xmxqlis.get(0).getXmdz());
        return prefix + "/edit";
    }
    
    /**
     * 修改设备材料合同出入库单
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(id);
        
        GcProjectSbclhtcrkditem items = new GcProjectSbclhtcrkditem();
        items.setSbclhtcrkdId(id);
        List<GcProjectSbclhtcrkditem> itemlis= gcProjectSbclhtcrkditemService.selectGcProjectSbclhtcrkditemList(items);
        gcProjectSbclhtcrkd.setItem(itemlis);
        
        mmap.put("gcProjectSbclhtcrkd", gcProjectSbclhtcrkd);
        return prefix + "/detail";
    }

    /**
     * 修改保存设备材料合同出入库单
     */
    @Log(title = "设备材料合同出入库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSbclhtcrkd gcProjectSbclhtcrkd){
    	try {
        	gcProjectSbclhtcrkditemService.deleteGcProjectSbclhtcrkditemById(gcProjectSbclhtcrkd.getId());
        	
        	double hjje = 0.0;
        	//item
        	for(GcProjectSbclhtcrkditem item : gcProjectSbclhtcrkd.getItem()){
        		item.setSbclhtId(gcProjectSbclhtcrkd.getSbclhtId());
        		item.setSbclhtcrkdId(gcProjectSbclhtcrkd.getId());
        		item.setDeleteFlag(1);
        		item.setCreateAt(new Date());
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setGcProjectid(gcProjectSbclhtcrkd.getGcProjectid());
        		double jine = DecimalCalculateUtil.mul(Double.parseDouble(item.getDanj().toString()), Double.parseDouble(item.getShul()));
        		hjje += jine;
        		item.setJine(Double.valueOf(jine));
        		gcProjectSbclhtcrkditemService.insertGcProjectSbclhtcrkditem(item);
        	}
        	
        	gcProjectSbclhtcrkd.setHej(Double.valueOf(hjje));
        	gcProjectSbclhtcrkd.setUpdateUser(getLoginName());
        	gcProjectSbclhtcrkd.setUpdateAt(new Date());
        	gcProjectSbclhtcrkdService.updateGcProjectSbclhtcrkd(gcProjectSbclhtcrkd);
		} catch (Exception e) {
			// TODO: handle exception
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 删除设备材料合同出入库单
     */
    @RequiresPermissions("gcgl:sbclhtcrkd:remove")
    @Log(title = "设备材料合同出入库单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSbclhtcrkdService.deleteGcProjectSbclhtcrkdByIds(ids));
    }
    
    //评审
    @RequiresPermissions("gcgl:sbclhtcrkd:psview")
    @GetMapping("/ps")
    public String sbclhtcrkdps(){
        return prefix + "/sbclhtcrkdps";
    }
    
    /**
     * 查询设备材料合同出入库单列表
     */
    @PostMapping("/pslist")
    @ResponseBody
    public TableDataInfo pslist(GcProjectSbclhtcrkd gcProjectSbclhtcrkd)
    {
        startPage();
        gcProjectSbclhtcrkd.setDeleteFlag(1);
        gcProjectSbclhtcrkd.setStatus(0);
        List<GcProjectSbclhtcrkd> list = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdList(gcProjectSbclhtcrkd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/yes")
    @ResponseBody
    public AjaxResult yes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(Long.valueOf(id[i].toString()));
    		if(gcProjectSbclhtcrkd!=null){
    			GcProjectSbclhtcrkd obj = new GcProjectSbclhtcrkd();
    			obj.setId(Long.parseLong(id[i]));
    			obj.setStatus(1);
    			obj.setCkzg(getLoginName());
    			obj.setCkzgAt(new Date());
        		gcProjectSbclhtcrkdService.updateGcProjectSbclhtcrkd(obj);
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/no")
    @ResponseBody
    public AjaxResult no(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(Long.valueOf(id[i].toString()));
    		if(gcProjectSbclhtcrkd!=null){
    			GcProjectSbclhtcrkd obj = new GcProjectSbclhtcrkd();
    			obj.setId(Long.parseLong(id[i]));
    			obj.setStatus(2);
    			obj.setCkzg(getLoginName());
    			obj.setCkzgAt(new Date());
        		gcProjectSbclhtcrkdService.updateGcProjectSbclhtcrkd(obj);
    		}
    	}
    	return toAjax(1);
    }
    
    //会计审核
    @RequiresPermissions("gcgl:sbclhtcrkd:hjpsview")
    @GetMapping("/hjps")
    public String hjps(){
        return prefix + "/sbclhtcrkdhjps";
    }
    
    /**
     * 查询设备材料合同出入库单列表
     */
    @PostMapping("/hjpslist")
    @ResponseBody
    public TableDataInfo hjpslist(GcProjectSbclhtcrkd gcProjectSbclhtcrkd)
    {
    	String[] statuslis = new String[]{"1","3"};
        startPage();
        gcProjectSbclhtcrkd.setDeleteFlag(1);
        gcProjectSbclhtcrkd.setStatuslis(statuslis);
        List<GcProjectSbclhtcrkd> list = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdList(gcProjectSbclhtcrkd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hjpsyes")
    @ResponseBody
    public AjaxResult hjpsyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(Long.valueOf(id[i].toString()));
    		if(gcProjectSbclhtcrkd!=null){
    			GcProjectSbclhtcrkd obj = new GcProjectSbclhtcrkd();
    			obj.setId(Long.parseLong(id[i]));
    			obj.setStatus(3);
    			obj.setType(1);
    			obj.setHuij(getLoginName());
    			obj.setHuijAt(new Date());
        		gcProjectSbclhtcrkdService.updateGcProjectSbclhtcrkd(obj);
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hjpsno")
    @ResponseBody
    public AjaxResult hjpsno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(Long.valueOf(id[i].toString()));
    		if(gcProjectSbclhtcrkd!=null){
    			GcProjectSbclhtcrkd obj = new GcProjectSbclhtcrkd();
    			obj.setId(Long.parseLong(id[i]));
    			obj.setStatus(4);
    			obj.setHuij(getLoginName());
    			obj.setHuijAt(new Date());
        		gcProjectSbclhtcrkdService.updateGcProjectSbclhtcrkd(obj);
    		}
    	}
    	return toAjax(1);
    }
    
    /**
     * 修改设备材料合同出入库单
     */
    @GetMapping("/rkprint/{id}")
    public String rkprint(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(id);
        
        GcProjectSbclhtcrkditem items = new GcProjectSbclhtcrkditem();
        items.setSbclhtcrkdId(id);
        List<GcProjectSbclhtcrkditem> itemlis= gcProjectSbclhtcrkditemService.selectGcProjectSbclhtcrkditemList(items);
        if(itemlis!=null && itemlis.size()<5){
        	for(int i=itemlis.size();i<5;i++){
        		GcProjectSbclhtcrkditem itemobj = new GcProjectSbclhtcrkditem();
        		itemobj.setXuh(i);
            	itemlis.add(itemobj);
        	}
        	
        	gcProjectSbclhtcrkd.setItem(itemlis);
        }else{
        	gcProjectSbclhtcrkd.setItem(itemlis);
        }
        
        mmap.put("gcProjectSbclhtcrkd", gcProjectSbclhtcrkd);
        mmap.put("riq", DateUtils.dateTime(gcProjectSbclhtcrkd.getRiq()) );
        return prefix + "/rkPrint";
    }
    
    @GetMapping("/ckprint/{id}")
    public String ckprint(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(id);
        
        GcProjectSbclhtcrkditem items = new GcProjectSbclhtcrkditem();
        items.setSbclhtcrkdId(id);
        List<GcProjectSbclhtcrkditem> itemlis= gcProjectSbclhtcrkditemService.selectGcProjectSbclhtcrkditemList(items);
        if(itemlis!=null && itemlis.size()<5){
        	for(int i=itemlis.size();i<5;i++){
        		GcProjectSbclhtcrkditem itemobj = new GcProjectSbclhtcrkditem();
        		itemobj.setXuh(i);
            	itemlis.add(itemobj);
        	}
        	
        	gcProjectSbclhtcrkd.setItem(itemlis);
        }else{
        	gcProjectSbclhtcrkd.setItem(itemlis);
        }
        
        mmap.put("gcProjectSbclhtcrkd", gcProjectSbclhtcrkd);
        mmap.put("riq", DateUtils.dateTime(gcProjectSbclhtcrkd.getRiq()) );
        return prefix + "/ckPrint";
    }
    
    /**
     * 总经理审批
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/rkzjlsp/{id}")
    public String rkzjlsp(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(id);
        
        GcProjectSbclhtcrkditem items = new GcProjectSbclhtcrkditem();
        items.setSbclhtcrkdId(id);
        List<GcProjectSbclhtcrkditem> itemlis= gcProjectSbclhtcrkditemService.selectGcProjectSbclhtcrkditemList(items);
        if(itemlis!=null && itemlis.size()<5){
        	for(int i=itemlis.size();i<5;i++){
        		GcProjectSbclhtcrkditem itemobj = new GcProjectSbclhtcrkditem();
        		itemobj.setXuh(i);
            	itemlis.add(itemobj);
        	}
        	
        	gcProjectSbclhtcrkd.setItem(itemlis);
        }else{
        	gcProjectSbclhtcrkd.setItem(itemlis);
        }
        
        mmap.put("gcProjectSbclhtcrkd", gcProjectSbclhtcrkd);
        mmap.put("riq", DateUtils.dateTime(gcProjectSbclhtcrkd.getRiq()) );
        return prefix + "/rkzjlsp";
    }
    
    /**
     * 会计审批
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/rkhjsp/{id}")
    public String rkhjsp(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectSbclhtcrkd gcProjectSbclhtcrkd = gcProjectSbclhtcrkdService.selectGcProjectSbclhtcrkdById(id);
        
        GcProjectSbclhtcrkditem items = new GcProjectSbclhtcrkditem();
        items.setSbclhtcrkdId(id);
        List<GcProjectSbclhtcrkditem> itemlis= gcProjectSbclhtcrkditemService.selectGcProjectSbclhtcrkditemList(items);
        if(itemlis!=null && itemlis.size()<5){
        	for(int i=itemlis.size();i<5;i++){
        		GcProjectSbclhtcrkditem itemobj = new GcProjectSbclhtcrkditem();
        		itemobj.setXuh(i);
            	itemlis.add(itemobj);
        	}
        	
        	gcProjectSbclhtcrkd.setItem(itemlis);
        }else{
        	gcProjectSbclhtcrkd.setItem(itemlis);
        }
        
        mmap.put("gcProjectSbclhtcrkd", gcProjectSbclhtcrkd);
        mmap.put("riq", DateUtils.dateTime(gcProjectSbclhtcrkd.getRiq()) );
        return prefix + "/rkhjsp";
    }
}